<template>
  <div v-if="hotCity">
    <div class="prefix">热门城市</div>
    <div class="hotcity-wrap">
      <div
        class="hotcity"
        v-for="val in hotCity.cities"
        @click="cityClick(val)"
      >
        {{ val.name }}
      </div>
    </div>
  </div>

  <div v-for="item in otherCity" class="city-wrap">
    <div class="prefix">{{ item.prefix.toUpperCase() }}</div>
    <div class="city" v-for="val in item.cities" @click="cityClick(val)">
      {{ val.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cityList: [],
      // hotCity: {},
      // otherCity: [],
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      fetch("http://118.24.239.63:3000/api/city/list")
        .then((response) => response.json())
        .then((res) => {
          if (res.success) {
            this.cityList = res.data;
            // this.hotCity = res.data[0];
            // this.otherCity = res.data.slice(1);
          }
        });
    },
    cityClick(val) {
      // 保存城市
      // console.log(val.name);
      this.$store.commit("changeName", val);

      // 跳转页面
      this.$router.push("/home");
    },
  },
  computed: {
    hotCity() {
      return this.cityList[0];
    },
    otherCity() {
      return this.cityList.slice(1);
    },
  },
};
</script>

<style lang="less" scoped>
.prefix {
  width: 375px;
  height: 30px;
  padding-left: 25px;
  background-color: rgb(235, 235, 235);
  font-size: 16px;
  line-height: 30px;
}
.city {
  margin-left: 15px;
  border-bottom: 1px solid @border-color;
  height: 45px;
  line-height: 45px;
  font-size: 16px;
}

.hotcity-wrap {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
}
.hotcity {
  width: 100px;
  height: 33px;
  line-height: 33px;
  text-align: center;
  font-size: 16px;
  border: 1px solid @border-color;
  margin: 10px 0;
}
</style>
